import 'package:uuid/uuid.dart';

class FileModel {
  static const tableName = "im_file";

  static const createTableSQL = """
      CREATE TABLE $tableName (
        id CHAR(32) PRIMARY KEY NOT NULL,
        mid CHAR(32) NOT NULL,
        isImage SMALLINT NOT NULL,
        fileName VARCHAR(100) NOT NULL,
        width INT NOT NULL,
        height INT NOT NULL,
        size INT NOT NULL,
        url VARCHAR(255) NOT NULL
      )""";

  static const columnSQL = "id, mid, isImage, fileName, width, height, size, url";

  static const insertSQL = """
  insert into $tableName ($columnSQL) values (?,?,?,?,?,?,?,?)
  """;

  static const updateSQL = """
  update $tableName set url = ? where id = ?
  """;

  static const String queryById = """
  select $columnSQL from $tableName where id = ?
  """;

  static const String queryByMid = """
  select $columnSQL from $tableName where mid = ?
  """;

  List<dynamic> getUpdateArgs() {
    return [url, id];
  }

  List<dynamic> getInsertArgs() {
    return [id, mid, isImage ? 1 : 0, fileName, width, height, size, url];
  }

  static FileModel fromJson(Map<String, dynamic> map) {
    return FileModel(
        id: map["id"],
        mid: map["mid"],
        isImage: map["isImage"] == 1 ? true : false,
        fileName: map["fileName"],
        width: map["width"],
        height: map["height"],
        size: map["size"],
        url: map["url"]);
  }

  String id;

  bool isImage;

  String fileName;

  int width;

  int height;

  int size;

  String url;

  String mid;

  FileModel(
      {this.id,
      this.isImage,
      this.fileName,
      this.mid,
      this.width = 0,
      this.height = 0,
      this.size = 0,
      this.url = ""}) {
    this.id = this.id == null ? Uuid().v4().replaceAll("-", "") : this.id;
  }

  @override
  String toString() {
    return 'FileModel{id: $id, isImage: $isImage, fileName: $fileName, width: $width, height: $height, size: $size, url: $url, mid: $mid}';
  }


}
